#pragma once
#include "DXAMLCommon.h"
#include "CXcpList.h"
#include "ICoreWindowRenderTarget.h"
#include "CRenderTarget.h"
#include "IPLMListener.h"


class VisualTree;


class CWindowRenderTarget :
	public ICoreWindowRenderTarget,
	public CRenderTarget,
	public IPLMListener
{
private: 
	CWindowRenderTarget(struct ICoreServices * pCore, XBOOL isPrimaryWindowTarget, struct ICoreCompositorScheduler * pICompositorScheduler, struct IPALGraphicsDeviceManager * pIGraphicsDeviceManager, struct IPALPresentTarget * pIInitialPresentTarget);
private: 
	virtual ~CWindowRenderTarget();
public: 
	virtual XUINT32 AddRef();
	virtual XUINT32 Release();
	static HRESULT Create(struct ICoreServices * pCore, XBOOL isPrimaryWindowTarget, struct ICoreCompositorScheduler * pICompositorScheduler, struct IPALGraphicsDeviceManager * pIGraphicsDeviceManager, struct IPALPresentTarget * pIInitialPresentTarget, CWindowRenderTarget * * ppWindowRenderTarget);
	virtual HRESULT RegisterGraphicsDeviceChangeListener(struct IPALGraphicsDeviceChangeListener * pListener);
	virtual HRESULT UnregisterGraphicsDeviceChangeListener(struct IPALGraphicsDeviceChangeListener * pListener);
	virtual HRESULT Present();
	virtual XBOOL IsUsingMultiplePresentTargets();
	virtual struct IPALDCompTreeHost * GetDCompTreeHost();
	virtual XUINT32 GetWidth() const;
	virtual XUINT32 GetHeight() const;
	virtual XBOOL IsRightAngleRotated() const;
	virtual HRESULT Retarget(struct IPALPresentTarget * pIPresentTarget);
	virtual HRESULT GetPresentTarget(struct IPALPresentTarget * * ppPresentTarget);
	virtual CHardwareCompositor * GetPrimaryCompositor() const;
	virtual HRESULT Draw(struct ICoreServices * pCore, XBOOL fForceRedraw, struct XRECT_WH * prcDirtyRect);
	HRESULT DrawVisualTree(XBOOL fForceRedraw, XUINT32 uBackgroundColor, VisualTree * pVisualTree);
	XBOOL HasRenderThreadFailed();
	HRESULT CleanUpResourcesAfterRenderThreadFailure();
	HRESULT RecoverAfterRenderThreadFailure();
	void ResetRenderThreadFailure();
	enum RenderThreadState GetRenderThreadState();
	HRESULT ResumeRenderThreadAfterNewFrame();
	virtual HRESULT EnsureD2DDeviceAndFactory();
	virtual void GetD2DFactory(struct IPALAcceleratedGraphicsFactory * * ppD2DFactory);
	virtual void GetD2DDevice(struct IPALOptimizedGraphicsDevice * * ppD2DDevice);
	virtual HRESULT OnSuspend();
	virtual HRESULT OnResume();
private: 
	HRESULT Initialize();
	HRESULT EnsurePresentMethod(XBOOL recreateCompositors);
	struct
	{
		XUINT32 m_isPrimaryWindowTarget:1;
		XUINT32 m_needsFullRedraw:1;
		XUINT32 m_needsToReleaseHardwareResources:1;
	};

private: 
	CCoreServices * m_pCoreNoRef;
private: 
	CXcpList<IPALGraphicsDeviceChangeListener> m_graphicsDeviceChangeListenersNoRef;
};

